package com.singleton;

/**
 * @Classname Mar03
 * @Description 懒汉式
 * 1、线程不安全如果增加线程后对象就会从新new出来
 * @Date 2022/4/26 23:48
 * @Created by 666
 */
public class Mar03 {
    /**
     * 常量对象
     */
    private static Mar03 INSTANCE;

    private Mar03() {
    }

    public static Mar03 getInstance() {
        if (INSTANCE == null) {
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new Mar03();
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println(Mar03.getInstance().hashCode());
//                }
//            }).start();

            new Thread(() ->
                    System.out.println(Mar03.getInstance().hashCode())
            ).start();
        }
    }
}
